package sfeir.ergosum;

import sfeir.ergosum.service.BackUpService;
import sfeir.ergosum.service.ImportService;
import sfeir.ergosum.tools.HiveColorPickerActivity;
import sfeir.ergosum.tools.WallpaperActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;

public class PreferencesActivity extends PreferenceActivity {

   
    public static void startPreferenceAction(Context context) {
        context.startActivity(new Intent(context, PreferencesActivity.class));
    }

    public static void startPreferenceActionAuthentification(Context context) {
        Intent intent = new Intent(context, PreferencesActivity.class);
        intent.putExtra("authentification", true);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       
        addPreferencesFromResource(R.xml.preferences);
        findPreference("preference_backup").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                if (hasCredential()) {
                    BackUpService.actionBackup(PreferencesActivity.this);
                    return true;
                }
                return false;
            }

        });

        findPreference("preference_restore").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                if (hasCredential()) {
                    RestoreBackUpActivity.actionRestoreBackup(PreferencesActivity.this);
                    return true;
                }
                return false;
            }

        });

        findPreference("preference_import").setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
//                try {
//                    new ImportContacts(null).importAllContacts(PreferencesActivity.this);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                startService(new Intent(PreferencesActivity.this, ImportService.class));
                return true;
            }

        });

        findPreference("preference_color").setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivity(new Intent(PreferencesActivity.this, HiveColorPickerActivity.class));
                return true;
            }
        });

        findPreference("preference_wallpaper").setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivity(new Intent(PreferencesActivity.this, WallpaperActivity.class));
                return true;
            }
        });
    }

    protected boolean hasCredential() {
        final PreferencesLoginPass dia = (PreferencesLoginPass) findPreference("preference_loginpassKey");

        String ids = dia.getSharedPreferences().getString("preference_loginpassKey", " \n ");
        String[] values = ids.split("\n");

        if (values.length != 2 || " ".equals(values[0]) || " ".equals(values[1])) {
            new AlertDialog.Builder(this).setIcon(android.R.drawable.stat_notify_error).setMessage(
                    getString(R.string.profile_fill_in_authentification)).setPositiveButton(
                    getString(R.string.preference_go_to_pref_screen), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dia.openDialog();
                        }
                    }).setCancelable(true).setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    finish();
                }
            }).show();
            // Toast.makeText(this, "Fill in you credentials first", 3000);
            return false;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        if (intent.hasExtra("authentification") && intent.getBooleanExtra("authentification", false)) {
            PreferencesLoginPass dia = (PreferencesLoginPass) findPreference("preference_loginpassKey");
            dia.openDialog();
        }
    }
}
